package com.javasea.spring.proxy.cglibproxy;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.cglib.proxy.InvocationHandler;
import org.springframework.cglib.proxy.Proxy;

public class ProxyFactoryBean implements FactoryBean {
    /**
     * 返回bean实例对象
     * 代理的对象 IUserDao 放到了 getObject() 方法中，那么现在再从 Spring 中获取到的对象，就是我们的代理对象了
     * @return
     * @throws Exception
     */
    @Override
    public Object getObject() throws Exception {

        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        Class[] classes = {IUserDao.class};
        InvocationHandler handler = (proxy, method, args) -> "你被代理了 " + method.getName();

        return Proxy.newProxyInstance(classLoader, classes, handler);
    }

    /**
     * 返回实例类类型
     * @return
     */
    @Override
    public Class<?> getObjectType() {
        return IUserDao.class;
    }

}
